<?




include_once("base.php");

//=======================================
//=======================================
// 所属路径=$_REQUEST['path'];
// 文章名字=$_REQUEST['name'];
// 文章id  =$_REQUEST['nid'];
//=======================================
//=======================================



$_config=select_one_line_all_items($table_prefix."wechat_config","1=1");

//print_r2($_config);


//看看是不是要拿token

if  ( ($_config['exptime']<time()) || (empty($_config['exptime']))  )
{
    //echo "过期";
    $newset['ai_token']=wechat_get_token($_config);
    $newset['ai_exptime']=time()+7200;

    $_renew_config=update_date($table_prefix."wechat_config",$newset,"appid",$_config['appid']);
    $query=mysql_query($_renew_config) or die (mysql_error());

    $newest_token=$newset['ai_token'];
}
else
{
    //echo "未过期";
    $newest_token=$_config['token'];
}










function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                       'Content-Type: application/json; charset=utf-8',
                       'Content-Length: ' . strlen($jsonStr)
                   )
    );
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return array($httpCode, $response);
}




$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$newest_token;

$jsonStr = json_encode(array(
                           'path'         => $_REQUEST['path']."?nid=".$_REQUEST['nid']
                       ));


list($returnCode, $returnContent) = http_post_json($url, $jsonStr);


file_put_contents("/www/wwwroot/evolution/uploadfiles/".$theme_folder."/".$_REQUEST['name']."-".$_REQUEST['nid']."-".date("ydmdsi").".jpg", $returnContent);


$result="uploadfiles/".$theme_folder."/".$_REQUEST['name']."-".$_REQUEST['nid']."-".date("ydmdsi").".jpg";

echo out("success",0,$result);
 
?>